'''
# @Title:
# @Time : 2022/3/2 12:02
# @File : mitm_rewrite.py
# @Software: PyCharm

'''
import json

from mitmproxy import http,ctx


class MitmReWrite:

    # 因为是修改的内容是响应体，所以使用response事件
    def response(self, flow:http.HTTPFlow):
        # 如果list.json文件存在url中
        if "list.json" in flow.request.pretty_url:
            # 响应体转换为python对象
            data = json.loads(flow.response.content)
            # 修改第一条数据的name为：测试rewrite
            data["data"]["list"][0]["name"] = "测试rewrite"
            # 将data转换为json字符串，给response的text
            flow.response.text = json.dumps(data)


addons = [
    MitmReWrite()
]